<template>
  <el-dialog  :width="width" :visible="visible" :show-close="false" @close="$emit('close')">
    <div slot="title" class="title-wrapper">
      <div class="close-icon-wrapper" @click="$emit('close')">
        <i class="el-icon-arrow-left"></i>
      </div>
      <div class="title-text">
        {{ title }}
      </div>
    </div>
    <el-form ref="form" size="medium" :rules="rules" :model="form" :label-width="labelWidth">
      <el-row :gutter="15">
        <el-col :md="12" :sm="24">
          <el-form-item label="app版本" prop="version" >
            <el-input v-model="form.version" style="width: 90%" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :md="12" :sm="24">
          <el-form-item label="下载地址" prop="url" >
            <ad-upload v-model="form.url"/>
            <div>{{form.url}}</div>
          </el-form-item>
        </el-col>
        <el-col :md="12" :sm="24">
          <el-form-item label="强制更新" prop="isForce" >
            <el-select v-model="form.isForce" placeholder="请选择" style="width: 90%" clearable>
              <el-option
                v-for="item in optionsPerson"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <slot></slot>
    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button class="confirm-btn"  type="primary" @click="submit">确 定</el-button>
      <el-button class="cancel-btn" @click="$emit('close')">取 消</el-button>
      </span>
  </el-dialog>

</template>

<script>
// import {addAppVersion, editAppVersion} from '@/api/regionalManagement.js'
// import {client, getFileNameUUID, headers} from "@/utils/alioss";
// import setting from "@/config/setting";
import AdUpload from '@/components/AdUpload/index.vue'
import editor from '@/components/Editor/QuillEditor.vue'
import { appVersionAddItem, appVersionEdit } from '@/api/modular/system/loginManage'
export default {
  components: {
    AdUpload
  },
  props:{
    title: {
      type: String,
      default: ''
    },
    width:{
      type: String,
      default: '50%'
    },
    visible: {
      type: Boolean,
      default: false
    },
    labelWidth: {
      type: String,
      default: '120px'
    },
    form:{
      type: Object,
      default() {
        return {}
      }
    },
    rules:{
      type: Object,
      default() {
        return {}
      }
    },
    edit:{
      type: String,
      default: ''
    },
  },
  data(){
    return {
      percentage:0,
      progressA:false,
      pictureLoading:false,
      optionsPerson:[
        {
          label:'强制更新',
          value:1
        },
        {
          label:'不强制更新',
          value:2
        }
      ],
    }
  },
  methods: {
    // upload(file) {
    //  let  _this = this
    //   const tmpcnt = file.name.lastIndexOf(".");
    //   const suffix = file.name.substring(tmpcnt + 1);
    //   _this.pictureLoading = true;
    //   const fileName = getFileNameUUID() + "." + suffix;
    //   client().multipartUpload(fileName, file, {
    //       progress: function (percentage, cpt) {
    //           _this.progressA = true
    //           _this.percentage = Math.floor(percentage*100)
    //
    //         if (_this.percentage == 100){
    //           _this.progressA = false
    //         }
    //         console.log("打印进度",  percentage*100, _this);
    //       },
    //     },
    //
    //   ).then((res) => {
    //       this.form.url = "http://" + res.bucket + "." + setting.endpoint + "/" + res.name;
    //     this.pictureLoading = false;
    //   }).catch((err) => {
    //     this.pictureLoading = false;
    //   });
    // },
    handleRemove(file, fileList) {
      if (fileList && fileList.length === 0 ){
        this.form.url = ''
      }
    },
    handleChange(file) {
      const fileTemp = file.raw;
      if (fileTemp) {
        this.upload(fileTemp);
      } else {
        this.$message({
          type: "warning",
          message: "请上传文件！",
        });
      }
    },
    submit() {
      let form = {...this.form}
      this.$refs.form.validate(valid=> {
        if(valid) {
          if (!this.edit){
            appVersionAddItem({
              "version": form.version,
              "url": form.url,
              "isForce": form.isForce,
            }).then(response =>{
              this.$message.success('新增成功')
              this.$emit('close')
              this.$emit('getList')
            })
          }else {
            appVersionEdit({
              "delFlag": 1,
              "id": form.id,
              "version": form.version,
              "url": form.url,
              "isForce": form.isForce,
            }).then(response =>{
              this.$message.success('编辑成功')
              this.$emit('close')
              this.$emit('getList')
            })
          }
        }
      })
    }
  }
}
</script>


<style lang="scss" scoped>
::v-deep .el-dialog__header {
  padding: 0 !important;
  height: 66px;
  background: linear-gradient(
      148deg,
      #3aa1fe 0%,
      #4679fe 28%,
      #3aa1fe 54%,
      #3aa1fe 79%,
      #1753e2 100%
  );
}

::v-deep .el-dialog__body {
  padding-top: 40px !important;
}
::v-deep .el-dialog__footer {
  padding: 0 0 30px !important;
  border: none !important;
}

.title-wrapper {
  position: relative;
  height: 66px;
  display: flex;
  align-items: center;
  justify-content: center;

  .close-icon-wrapper {
    position: absolute;
    left: 0;
    top: 0;

    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 66px;
    color: #fff;
    //font-size: $xxl-font-size;
    cursor: pointer;
  }

  .title-text {
    //font-size: $lg-font-size;
    color: #ffffff;
  }
}

.dialog-footer {
  display: flex;
  justify-content: center;

  .confirm-btn {
    margin-right: 118px;
    width: 150px;
    height: 42px;
    background: #3aa1fe;
    border-radius: 4px 4px 4px 4px;
    color: #ffffff;
  }

  .cancel-btn {
    width: 150px;
    height: 42px;
    background: #9cafc6;
    border-radius: 4px 4px 4px 4px;
    color: #ffffff;
  }
}
</style>

